/*
 * 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
 * 开发板官网：www.lckfb.com
 * 技术支持常驻论坛，任何技术问题欢迎随时交流学习
 * 立创论坛：https://oshwhub.com/forum
 * 关注bilibili账号：【立创开发板】，掌握我们的最新动态！
 * 不靠卖板赚钱，以培养中国工程师为己任
 * Change Logs:
 * Date           Author       Notes
 * 2024-06-12     LCKFB-LP    first version
 */
#include "board.h"
#include "stdio.h"
#include "bsp_uart.h"
#include "lcd.h"
#include "ina226.h"
#include "lcd_init.h"
#include "logo.h"

float Current;
float Voltage;
float Power;
double Energy;
uint32_t Time_ms;


int32_t main(void)
{

    board_init();	// 开发板初始化
    uart1_init(115200);	// 串口1波特率115200
//	  LCD_Init();
//    LCD_Fill(0,0,160,80,BLACK);
//		PWM_Init(20);
//    PWM_Control(20);

//flash均衡损耗算法------------------************************************************************
    CW_SYSCTRL->AHBEN_f.FLASH = 1;
    for(uint16_t flash_addr =0; flash_addr<255; flash_addr++)
    {
        if(*((volatile uint16_t *)(512 * 127 + (flash_addr*2)))==0xffff)
        {
            if(flash_addr==0)
            {
                gui_flag.brightness_flag=20;
                gui_flag.gui_none_flag=0;
                FLASH_UnlockPages(512 * 127, 512 * 127);  //解锁最后一个页面
                FLASH_WriteBytes(512 * 127,gui_flag.gui_all_flag_8,2);
                FLASH_LockAllPages();
            }
            else
            {
                gui_flag.gui_all_flag=*((volatile uint16_t *)(512 * 127 + (flash_addr*2)-2));
//								LCD_ShowIntNum(0,0,flash_addr,4,RED,BLACK,16);
//								delay_ms(500);
                break;
            }
        }
        else if(flash_addr==254)
        {
            FLASH_UnlockPages(512 * 127, 512 * 127);  //解锁最后一个页面
            FLASH_ErasePages(512 * 127, 512 * 127);   //擦除最后一个页面
            FLASH_LockAllPages();
            flash_addr=0;
        }
//				LCD_ShowIntNum(0,0,flash_addr,4,RED,BLACK,16);
//				delay_ms(500);
    }
//flash均衡损耗算法------------------************************************************************
//懒得封装了

    LCD_Init();
    LCD_Fill(0,0,160,80,BLACK);
    INA226_Init();
    char Current_buf[12];
    char Voltage_buf[12];
    char Power_buf[12];
    char Energy_buf[12];
    char Time_ms_buf[12];
    PWM_Init(20);
    PWM_Control(gui_flag.brightness_flag);
    LCD_ShowPicture(0,0,160,80,gImage_logo);
    delay_ms(1000);
    LCD_Fill(0,0,160,80,BLACK);
    user_button_init();
    tim_init();
    // gui_flag.rotation_flag=0;
    uint8_t main_page_flag;
    while(1)
    {
        if(key_flag.key_0_1_meantime_flag)
        {
            key_flag.key_all_flag=0;
            key_flag.key_0_no_response_flag=1;
            key_flag.key_1_no_response_flag=1;
            gui_menu();

        }
        else if(key_flag.key_0_flag&(!key_flag.key_0_1_meantime_flag))
        {
            if(key_flag.key_0_no_response_flag)
            {
                key_flag.key_0_no_response_flag=0;
            }
            else
            {
                LCD_Fill(0,0,160,80,BLACK);
                main_page_flag=!main_page_flag;
            }
            key_flag.key_0_flag=0;
        }
        else if (key_flag.key_1_flag&(!key_flag.key_0_1_meantime_flag))
        {
            if(key_flag.key_1_no_response_flag)
            {
                key_flag.key_1_no_response_flag=0;
            }
            else
            {
                if(!main_page_flag)
                {
                    Energy=0;
                    Time_ms=0;
                }

            }
            key_flag.key_1_flag=0;
        }
        if(gui_flag.uint_flag)
        {
            if(!main_page_flag)
            {
                sprintf(Energy_buf,"%10.5fWH",Energy/1000);
                LCD_ShowString(0,10,Energy_buf,MAGENTA,BLACK,24,0);
                sprintf(Time_ms_buf, "%02d:%02d:%02d",
                        (Time_ms / 3600000),            // 小时
                        (Time_ms / 60000) % 60,        // 分钟
                        (Time_ms / 1000) % 60);        // 秒
//                sprintf(Time_ms_buf,"%10dms",Time_ms);
                LCD_ShowString(36,43,Time_ms_buf,CYAN,BLACK,24,0);
            }
            else
            {
                sprintf(Voltage_buf,"%11.5fV",Voltage/1000);
                LCD_ShowString(0,3,Voltage_buf,RED,BLACK,24,0);
                sprintf(Current_buf,"%11.5fA",Current/1000);
                LCD_ShowString(0,27,Current_buf,GREEN,BLACK,24,0);
                sprintf(Power_buf,"%11.5fW",Power/1000);
                LCD_ShowString(0,51,Power_buf,BLUE,BLACK,24,0);
            }
        }
        else
        {
            if(!main_page_flag)
            {
                sprintf(Energy_buf,"%9.2fmWH",Energy);
                LCD_ShowString(0,10,Energy_buf,MAGENTA,BLACK,24,0);
                sprintf(Time_ms_buf, "%02d:%02d:%02d",
                        (Time_ms / 3600000),            // 小时
                        (Time_ms / 60000) % 60,        // 分钟
                        (Time_ms / 1000) % 60);        // 秒
//                sprintf(Time_ms_buf,"%10dms",Time_ms);
                LCD_ShowString(36,43,Time_ms_buf,CYAN,BLACK,24,0);
            }
            else
            {
                sprintf(Voltage_buf,"%10.2fmV",Voltage);
                LCD_ShowString(0,3,Voltage_buf,RED,BLACK,24,0);
                sprintf(Current_buf,"%10.2fmA",Current);
                LCD_ShowString(0,27,Current_buf,GREEN,BLACK,24,0);
                sprintf(Power_buf,"%10.2fmW",Power);
                LCD_ShowString(0,51,Power_buf,BLUE,BLACK,24,0);
            }
        }

    }
}
